package andy.ham;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * Created by Crazy on 2018/6/19.
 */

public class LoginActivity extends AppCompatActivity{

    private final String TAG = "LoginActivity";

    EditText Euesrname;
    EditText Epassword;
    TextView login;
    TextView regist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        setTitle("登录");


        //匹配id
        Euesrname = (EditText)findViewById(R.id.M_username);
        Epassword = (EditText)findViewById(R.id.M_password);
        login = (TextView)findViewById(R.id.M_login);
        regist = (TextView)findViewById(R.id.M_regist);


        //点击登录按钮
        login.setOnClickListener(new TextView.OnClickListener(){
            @Override
            public void onClick(View view) {

                //账号密码匹配
                String username = Euesrname.getText().toString();
                String password = Epassword.getText().toString();

                //判断账号密码是否为空
                if (username.isEmpty()||password.isEmpty()){
                    Toast.makeText(LoginActivity.this, "账号和密码皆不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                //数据库中查找所有数据填入list
                List<user> users = DataSupport.findAll(user.class);
                //运用for循环判断是否有存在账号与输入内容相等
                for(user user : users){
                    if(user.getUsername().equals(username)){
                        //判断有此账号后校对密码是否正确
                        if (user.getPassword().equals(password)){
                            Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            intent.setClass(LoginActivity.this,MyDiary.class);
                            startActivity(intent);
                            LoginActivity.this.finish();
                            Log.d(TAG, "username is "+user.getUsername());
                            Log.d(TAG, "password is "+user.getPassword());
                            return;
                        }
                        else{
                            Toast.makeText(LoginActivity.this, "密码输入错误！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                }
                Toast.makeText(LoginActivity.this, "账号输入错误！", Toast.LENGTH_SHORT).show();

            }
        });

        //点击注册按钮，跳转注册界面
        regist.setOnClickListener(new TextView.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,AssignActivity.class);
                startActivity(intent);
            }
        });
    }
}
